package org.spring.springboot.util.mail;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.Properties;

@Component
public class SendEMail {
    @Value("${mail.transport.protocol}")
    private static String protocol;
    @Value("${mail.smtp.host}")
    private static String host;
    @Value("${mail.smtp.auth}")
    private static String auth;
    @Value("${accout}")
    private static String accout;
    @Value("${password}")
    private static String password;
    public static void main(String[] args) throws Exception{
      /*  String accout="18323293691@163.com";
        String password="12020709ty";
        StringBuilder html=new StringBuilder();
        html.append("<html>");
        html.append("<body>");
        html.append("<a href=''>这是测试哈哈吼吼</a>");
        html.append("</body>");
        html.append("</html>");
        MimeMessage m=createEmail(accout,password,"这是主题",html.toString(),"1218226694@qq.com");
        Session session=m.getSession();
        Properties config=session.getProperties();
        config.put("mail.transport.protocol","smtp");
        config.put("mail.smtp.host","smtp.163.com");
        //config.put("mail.smtp.port","25");
        config.put("mail.smtp.auth","true");
        //config.put("mail.smtp.ssl.enable","false");*/


        /*String accout="18323293691@163.com";
        String password="12020709ty";*/
        StringBuilder html=new StringBuilder();
        html.append("<html>");
        html.append("<body>");
        html.append("<a href=''>这是测试</a>");
        html.append("</body>");
        html.append("</html>");
        MimeMessage m=createEmail(SendEMail.accout,SendEMail.password,"这是主题",html.toString(),"1218226694@qq.com");
        Session session=m.getSession();
        Properties config=session.getProperties();
        config.put("mail.transport.protocol",SendEMail.protocol);
        config.put("mail.smtp.host",SendEMail.host);
        //config.put("mail.smtp.port","25");
        config.put("mail.smtp.auth",SendEMail.auth);
        //config.put("mail.smtp.ssl.enable","false");
        Transport.send(m);
    }
    /**
     * accout：发送人账号
     *password：发送人密码
     * mailtheme: 邮件主题
     * html:邮件内容(发送html)
     * Addressee：收件人
     */
    public static MimeMessage createEmail( final String accout, final String password,String mailtheme,String html,String Addressee) throws Exception{
        Properties config=new Properties();
        //验证邮件账号和秘密
        Session session= Session.getInstance(config, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(accout,password);
            }
        });
        //1创建邮件对
        MimeMessage mailMessage = new MimeMessage(session);
        //2设置发件人
        Address from=new InternetAddress(accout,"GAT","UTF-8");
        mailMessage.setFrom(from);
        //3设置收件人
        mailMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(Addressee));
        //4设置超送人
        //mailMessage.setRecipients(RecipientType.CC, new InternetAddress("121255@qq.com"));
        //5设置秘送人
        //mailMessage.setRecipients(RecipientType.BCC, new InternetAddress("121255@qq.com"));
        //6邮件发送主题
        mailMessage.setSubject(mailtheme,"UTF-8");

        //7设置邮件正文
       /* StringBuilder html=new StringBuilder();
        html.append("<html>");
        html.append("<body>");
        html.append("<a href=''>这是测试</a>");
        html.append("</body>");
        html.append("</html>");*/

        mailMessage.setContent(html,"text/html; charset=utf-8");  //text/plain法文本
        //8设置邮件发送时间
        mailMessage.setSentDate(new Date());
        //确认设置完毕
        mailMessage.saveChanges();
        return mailMessage;
    }
}
